package com.ling.pl.core.commons.exception;

import com.ling.pl.core.enums.ErrorCode;

/**
 * 基本异常类,抛出此异常,事物不回滚
 *
 * @author dada
 */
public class BusinessContinueException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private ErrorCode errorCode = ErrorCode.UNKNOW;

    private String errorMessage = null;

    /**
     * 业务异常构造器，传入错误代码和源异常
     **/
    public BusinessContinueException(ErrorCode errorCode, Throwable t) {
        super(t);
        this.errorCode = errorCode;
        this.errorMessage = "[" + errorCode.getMessage() + "]";
    }

    /**
     * 业务异常构造器，传入错误信息和源异常
     **/
    public BusinessContinueException(String errorMessage, Throwable t) {
        super(t);
        this.errorMessage = errorMessage;
    }

    /**
     * 业务异常构造器，传入错误代码和错误信息和源异常
     **/
    public BusinessContinueException(ErrorCode errorCode, String errorMessage, Throwable t) {
        super(t);
        this.errorCode = errorCode;
        this.errorMessage = "[" + errorCode.getMessage() + "] - " + errorMessage;
    }

    /**
     * 业务异常构造器，传入错误代码和错误信息
     **/
    public BusinessContinueException(ErrorCode errorCode, String errorMessage) {
        this.errorCode = errorCode;
        this.errorMessage = "[" + errorCode.getMessage() + "] - " + errorMessage;
    }

    /**
     * 业务异常构造器，传入错误信息
     **/
    public BusinessContinueException(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public ErrorCode getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(ErrorCode errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    /**
     * 返回异常信息
     *
     * @return
     * @author dada
     */
    public String getMessage() {
        if (this.errorMessage != null) {
            return this.errorMessage;
        } else {
            return "未知的错误";
        }
    }

    /**
     * 转化为字符串的方法
     *
     * @author dada
     */
    public String toString() {
        String s = getClass().getName();
        String message = getMessage();
        return (message != null) ? (s + ": " + message) : s;
    }
}
